package org.fuxian.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.dao.CarouselImgMapper;
import org.fuxian.model.CarouselImg;
import org.fuxian.util.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    CarouselImgMapper carouselImgMapper;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", "200");

        PageHelper.startPage(page, 5);
        List<CarouselImg> imgList = carouselImgMapper.selectAll();
        PageInfo<CarouselImg> pageInfo = new PageInfo<>(imgList, 5);
        returnMap.put("pageInfo", pageInfo);
        return returnMap;
    }


    @RequestMapping("/getOne")
    @ResponseBody
    public Map<String, Object> getOne(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        CarouselImg carouselImg = carouselImgMapper.selectByPrimaryKey(id);

        if (carouselImg != null) {
            map.put("code", "200");
            map.put("carouselImg", carouselImg);
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody(required = true) CarouselImg carouselImg) {
        Map<String, Object> map = new HashMap<>();
        int count = carouselImgMapper.updateByPrimaryKey(carouselImg);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }

    @RequestMapping("/new")
    @ResponseBody
    public Map<String, Object> insert( @RequestParam(value = "desc") String desc, @RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String newPath = "img"+ File.separator ;
        newPath += UploadUtils.upload(request, file);
        CarouselImg carouselImg = new CarouselImg();
        carouselImg.setDescription(desc);
        carouselImg.setName(newPath);
        carouselImg.setFlag(1);
        int count = carouselImgMapper.insert(carouselImg);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }


    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        int count = carouselImgMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            map.put("code", "200");
        } else {
            map.put("code", "500");
        }
        return map;
    }


}
